{include file="public/head"}
<div class="main">
    {include file="public/left"}
    <div class="middle">
    {include file="public/header"}
    <ul class="release_tab">
        <dl>
            <span>{:__('发布任务只需')}</span>
            <span class="num">3</span>
            <span>{:__('步')}</span>
        </dl>
        <dl>{:__('选择任务模式')}<span class="num2">1</span></dl>
        <dl>{:__('描述任务需求')}<span class="num2">2</span></dl>
        <dt>{:__('任务订单确认')}<span class="num1">3</span></dt>
    </ul>
    <ul class='order_confirm'>
        <h3>{:__('任务订单确认')}</h3>
        <div class='order_tab'>
            <h2>{:__('订单列表')}</h2>
            <dl>
                <span>{:__('收费项目')}</span><span style="width:28%">{:__('价格')}|{:__('总计')}<a class='money_show' style="color: #0066CB;">0</a></span><span style="text-align: right;">{:__('操作')}</span>
                <p>
                    <span><a id="title">XXXX</a></span><span style="width:55%"><a class='money_show' style="color: #666">0.00</a>$</span>
                    <span id="edittask" style="cursor: pointer;border-radius: 5px;padding: 2px 5px;width: auto;background-color: #690;color: white !important;font-size: 13px;">{:__('返回编辑')}</span>
                </p>
            </dl>
        </div>
        <div class="order_btn">
            <button class="layui-btn layui-btn-primary">{:__('确认发布')}</button>
        </div>
    </ul>
    {include file="public/foot"}
    </div>
    {include file="public/right"}
</div>
{include file="public/footer"}
<script>
    noLog()
    taskinfo = localStorage.getItem("taskinfo");
    formdata = JSON.parse(taskinfo);
    $("#title").html(formdata['title']);
    $(".money_show").html(formdata['money']);
    $(".order_btn button").click(function(){
        var _this = $(this);
        _this.attr("disabled",true);
        _this.removeClass("layui-btn-primary");
        _this.addClass("layui-btn-disabled");
        var cat_id_1 = formdata['cat_id_1'];
        cat_id_1 = parseInt(cat_id_1);
        if(cat_id_1 <= 0){
            layer.msg("{:__('任务分类不能为空')}",{icon:3});
            return false;
        }
        var cat_id = formdata["cat_id"];
        cat_id = parseInt(cat_id);
        if(cat_id <= 0){
            layer.msg("{:__('任务分类子行业不能为空')}",{icon:3});
            return false;
        }
        var title = formdata["title"];
        if(title == '' || title == null || title == undefined){
            layer.msg("{:__('任务标题不能为空')}",{icon:3});
            return false;
        }
        titletmp = title.replace(/ /g,'').replace(/&nbsp;/g,'');
        if(titletmp.length>50){
            layer.msg("{:__('任务标题不能大于50个字符')}",{icon:3});
            return false;
        }
        var content = formdata['content'];
        if(content == '' || content == null || content == undefined){
            layer.msg("{:__('任务描述不能为空')}",{icon:3});
            return false;
        }
        var contenttmp = content.replace(/ /g,'').replace(/&nbsp;/g,'');
        if(contenttmp.length<10){
            layer.msg("{:__('任务描述不能小于10个字符')}",{icon:3});
            return false;
        }
        var attach = formdata["attach"];
        if(attach == '' || attach == null || attach == undefined){
            attach = '';
        }
        formdata['attach_id'] = attach;
        var  money = formdata["money"];
        moneycopy = parseInt(money);
        if(money == '' || money == null || money == undefined || money == 0){
            layer.msg("{:__('任务金额不能为空或0')}",{icon:3});
            return false;
        }
        if(money != moneycopy){
            layer.msg("{:__('任务金额只能为整数')}",{icon:3});
            return false;
        }
        var slt_bid_mode = formdata['money_cfg']["slt_bid_mode"];
        slt_bid_mode = parseInt(slt_bid_mode);
        if(slt_bid_mode == 2){
            var money_flag = 0;
            var people_flag = 0;
            var len = formdata["money_setting"];
            var allmoney = 0;
            for(i=1;i<len;i++){
                // 平分奖金校验
                var txt_avg_money = formdata['money_cfg']["txt_avg_money_"+i];
                var txt_avg_moneyint = parseInt(txt_avg_money);
                if(txt_avg_money == '' || txt_avg_money == null || txt_avg_money == undefined || txt_avg_money == 0){
                    money_flag = 1;
                }
                if(txt_avg_money != txt_avg_moneyint){
                    money_flag = 1;
                }
                // 人数校验
                var txt_pople = formdata['money_cfg']["txt_pople_"+i];
                var txt_popleint = parseInt(txt_pople);
                if(txt_pople == '' || txt_pople == null || txt_pople == undefined || txt_pople == 0){
                    people_flag = 1;
                }
                if(txt_pople != txt_popleint){
                    people_flag = 1;
                }
                allmoney += txt_avg_moneyint*txt_popleint;
            }
            if(people_flag == 1){
                layer.msg("{:__('得奖人数不能为空或者0且只能整数')}",{icon:3});
                return false;
            }
            if(money_flag == 1){
                layer.msg("{:__('平分奖金不能为空或者0且只能整数')}",{icon:3});
                return false;
            }
            if(parseInt(allmoney)>parseInt(money)){
                layer.msg("{:__('多人中标总金额不能大于任务金额')}",{icon:3});
                return false;
            }

        } else if(slt_bid_mode == 3){
            var gj_money = formdata['money_cfg']["gj_money"];
            var gj_moneyint = parseInt(gj_money);
            if(gj_money == '' || gj_money == null || gj_money == undefined || gj_money == 0){
                layer.msg("{:__('计件金额不能为空或0')}",{icon:3});
                return false;
            }
            if(gj_money != gj_moneyint){
                layer.msg("{:__('计件金额只能为整数')}",{icon:3});
                return false;
            }
            var gj_num = formdata['money_cfg']["gj_num"];
            var gj_numint = parseInt(gj_num);
            if(gj_num == '' || gj_num == null || gj_num == undefined || gj_num == 0){
                layer.msg("{:__('计件数不能为空或0')}",{icon:3});
                return false;
            }
            if(gj_num != gj_numint){
                layer.msg("{:__('计件数只能为整数')}",{icon:3});
                return false;
            }
            var allmoney = gj_moneyint*gj_numint;
            if(parseInt(allmoney)>parseInt(money)){
                layer.msg("{:__('计件中标总金额不能大于任务金额')}",{icon:3});
                return false;
            }
        } else if(slt_bid_mode == 1){

        } else {
            layer.msg("{:__('页面加载失败请刷新界面重新操作')}",{icon:3});
            return false;
        }
        var days_expire = formdata["days_expire"];
        days_expireint = parseInt(days_expire);
        if(days_expire == '' || days_expire == null || days_expire == undefined || days_expire == 0){
            layer.msg("{:__('任务周期不能为空或0')}",{icon:3});
            return false;
        }
        if(days_expire != days_expireint){
            layer.msg("{:__('任务周期只能为整数')}",{icon:3});
            return false;
        }
        if(days_expireint>999){
            layer.msg("{:__('任务周期只能为三位数')}",{icon:3});
            return false;
        }
        var sign_limit = formdata["sign_limit"];
        sign_limitint = parseInt(sign_limit);
        if(sign_limit == '' || sign_limit == null || sign_limit == undefined || sign_limit == 0){
            layer.msg("{:__('报名人数限制不能为空或0')}",{icon:3});
            return false;
        }
        if(sign_limit != sign_limitint){
            layer.msg("{:__('报名人数限制只能为整数')}",{icon:3});
            return false;
        }
        if(sign_limitint>9999){
            layer.msg("{:__('报名人数限制不能超过9999')}",{icon:3});
            return false;
        }
         $.ajax({
            url:'/api/mission/create',
            type:'post',
            data:formdata,
            success:function(ret){
                if(ret.code == 1){
                    if(ret['data']['payed'] == 0){
                        layer.msg("{:__('余额不足请充值')}",{icon:3},function(){
                            window.location.href="/index/User/online?from=1&money="+moneycopy;
                            localStorage.removeItem("taskinfo");
                        });
                    } else {
                        layer.msg(ret.msg,{icon:1},function(){
                            window.location.href="/index/User/taskpub";
                            localStorage.removeItem("taskinfo");
                        });
                    }
                } else if(ret.code !=401){
                    layer.msg(ret.msg,{icon:2});
                }
            }
        });

    });
    $("#edittask").click(function(){
        window.location.href="/index/Task/tasksub?option=edit";
    });
</script>